package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.TccLogisticsPaymentTariff;
import com.ys.entity.sys.YsProcInst;
import com.ys.service.flow.YspTaskService;
import com.ys.service.storage.TccLogisticsPaymentTariffService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;
import com.ys.vo.Flow;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Author: xjx
 * @CreateTime: 2025-3-04
 * @Description: 物流付款单（关税费）
 * @Version: 1.5
 */
@Controller
@RequestMapping("/web/storage/tccLogisticsPaymentTariff")
public class WebTccLogisticsPaymentTariffController {
    private static final Logger logger = LoggerFactory.getLogger(WebTccLogisticsPaymentTariffController.class);

    @Autowired
    private CommonApiService<TccLogisticsPaymentTariff> commonApiService;

    @Autowired
    private TccLogisticsPaymentTariffService tccLogisticsPaymentTariffService;

    @Autowired
    private YspTaskService yspTaskService;

    //菜单id
    private Long menuId = MenuId.TCC_LogisticsPaymentTariff;
    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            TccLogisticsPaymentTariff entity = new TccLogisticsPaymentTariff();
            String bid = commonApiService.getCommonNextBid(menuId);
            entity.setBid(bid);
            Map<String,Object> extraParam = new HashMap<>();
            extraParam.put("isPublicPay",0);//是否对公支付

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,new ArrayList<>());
            return commonApiService.toAdd(model, entity, menuId, extraParam, subGridConfig);

        }catch (Exception e) {
            logger.error("页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,String bid) {
        try {
            TccLogisticsPaymentTariff entity = new TccLogisticsPaymentTariff().setBid(bid).queryFirst();

            List<Map<String, Object>> itemList = tccLogisticsPaymentTariffService.getItemList(bid);
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(menuId,itemList);

            Integer state = yspTaskService.flowState(bid);
            if (state == Flow.State.FINISH.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }else if (state == Flow.State.PROCESS.value){
                subGridConfig.setButtonArr(new ArrayList<>());
            }
            //获取审核时间
            YsProcInst ysProcInst = new YsProcInst().setBid(entity.getBid()).queryFirst();
            Map<String, Object> extraParam = new HashMap<>();
            extraParam.put("_ys_auditingTime", DateUtil.getYYYY_MM_DD_HH_mm_ss(ysProcInst.getUpdateTime()));

            return commonApiService.toEdit(model, entity.getId(), new TccLogisticsPaymentTariff(), menuId, extraParam, subGridConfig);

        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }


  
}

